// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopArr: [],
    totalPrice: 0,
    check: "1",
    message: "",
    index: '',
    username: null,
    tel: null,
    region: null,
    detailaddress: null
  },
  onClickButton(e) {
    if(this.data.index){
    console.log(e);
    console.log(this.data.shopArr);
    let { title, total } = JSON.parse(e.currentTarget.dataset.submits);
    console.log(title,total);
    wx.orderRequestPayment({
      timeStamp: "111",
      nonceStr: "222",
      _package: "prepay_id=dsfds",
      signType: "MD5",
      paySign: title + "##" + total + "##" + "234##dsg##fs##fff",
      success(res) {
        console.log(res);
      },
      fail(err) {
        console.log(err);
      }
    })
    }else{
      wx.showToast({
        title: '请选择收获地址',
        icon:"error"
      })
    }
  },


  messagefn(e) {
    console.log(e)
    const message = e.detail.value;
    this.setData({
      message
    })
  },
  //
  onChangeCheck(e) {
    console.log(e)
    let check = e ? e.detail : "1";
    this.setData({
      check
    })

  },

  //总结计算
  getTotal() {
    const totalPrice = this.data.shopArr.reduce((pre, cur) => {
      return pre + cur.price * cur.num
    }, 0)
    console.log(totalPrice)
    this.setData({
      totalPrice: totalPrice * 100
    })
  },
  //获取数据
  getshopArr() {
    let arr = wx.getStorageSync('yougouProduct').filter(item => item.check == true)
    console.log(arr)
    this.setData({
      shopArr: arr
    })
  },
  getAddress() {
    console.log(1)
    console.log(this.data.index)
    if (this.data.index) {
      console.log(2)
      let arr = wx.getStorageSync('userAddress')
      let index = this.data.index
      console.log(index)
      let { username, tel, region, detailaddress } = arr[index]
      this.setData({
        username, tel, region, detailaddress
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getshopArr()
    this.getTotal()
    this.onChangeCheck()
    this.setData({
      index: options.index
    })
    this.getAddress()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})